/*
 * Copyright (C) 2010 Cyril Mottier (http://www.cyrilmottier.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.greendroid;

import java.util.List;

import com.qingshui.browser.R;

import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * A {@link QuickActionLinear} is an implementation of a {@link QuickActionWidget}
 * that displays {@link QuickAction}s in a grid manner. This is usually used to create
 * a shortcut to jump between different type of information on screen.
 *
 * @author Benjamin Fellous
 * @author Cyril Mottier
 */
public class QuickActionLinear extends QuickActionWidget {

	private LinearLayout mLinearLayout;

	public QuickActionLinear(Context context) {
		super(context);

		setContentView(R.layout.gd_quick_action_linear);

		final View v = getContentView();
		mLinearLayout = (LinearLayout) v.findViewById(R.id.gdQuickActionLinear);
		
	}


	@Override
	protected void populateQuickActions(final List<QuickAction> quickActions) {
		final LayoutInflater inflater = LayoutInflater.from(getContext());
		//LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.gd_quick_action_list_item, mLinearLayout, false);
		
		if(mLinearLayout == null){
			return;
		}else{
			mLinearLayout.removeAllViews();
		}
		for(int i=0;i<quickActions.size();i++){
			
			LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.gd_quick_action_list_item, mLinearLayout, false);
			QuickAction quickAction = quickActions.get(i);
			TextView text = (TextView)linearLayout.findViewById(R.id.TabText);
			ImageView image=(ImageView)linearLayout.findViewById(R.id.TabImage);
			image.setImageDrawable(quickAction.mDrawable);
			
			text.setText(quickAction.mTitle);
			//���button��Ч��
			//Log.d("t", ""+linearLayout.getLayoutParams().width);
			Log.d("d", ""+i);
			mLinearLayout.addView(linearLayout,i);
		}
		
	}

	@Override
	protected void onMeasureAndLayout(Rect anchorRect, View contentView) {
		//this.setWidth(getScreenWidth());
		contentView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		contentView.measure(MeasureSpec.makeMeasureSpec(getScreenWidth(), MeasureSpec.EXACTLY),
				LayoutParams.WRAP_CONTENT//MeasureSpec.makeMeasureSpec(getScreenHeight()/2, MeasureSpec.EXACTLY)
				);
		int rootHeight =contentView.getMeasuredHeight();
		int offsetY = getArrowOffsetY();
		int dyTop = anchorRect.top;
		int dyBottom = getScreenHeight() - anchorRect.bottom;
		Log.d("anchor:",""+anchorRect.toShortString());
		boolean onTop = (dyTop > dyBottom);
		if(rootHeight>getScreenHeight()/2){
			rootHeight = getScreenHeight()/2;
			this.setHeight(rootHeight);
		}
		Log.d("anchor:","top "+anchorRect.top+"bottom "+anchorRect.bottom);
		
		int popupY = (onTop) ? anchorRect.top - rootHeight + offsetY : anchorRect.bottom - offsetY;
		Log.d("popupY:",""+popupY+" h:"+rootHeight);
		
		setWidgetSpecs(popupY, onTop);
		
	}

	@SuppressWarnings("unused")
	private OnItemClickListener mInternalItemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
			getOnQuickActionClickListener().onQuickActionClicked(QuickActionLinear.this, position);
			if (getDismissOnClick()) {
				dismiss();
			}
		}
	};

}

